package com.sdhs.analyze.utils;

import com.sdhs.analyze.bean.DicNodeType;
import com.sdhs.analyze.bean.InfoNodeStatus;
import com.sdhs.analyze.common.BizConstants;
import com.sdhs.analyze.service.monitor.NodeStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.Date;

@Component
public class StatusCheckUtils {

    @Autowired
    private NodeStatusService nodeStatusServiceTemp;

    private static NodeStatusService nodeStatusService;

    @PostConstruct
    public void init() {
        nodeStatusService = this.nodeStatusServiceTemp;
    }

    /**
     * 根据设定阈值判定状态
     * 区间1： 状态值 < keyStatus1
     * 区间2： keyStatus1 <= 状态值 < keyStatus2
     * 区间3： 状态值 >= keyStatus2
     * @return
     */
    public static InfoNodeStatus checkStatusByThreshold(
            Integer value, Date checkTime,
            DicNodeType dicNodeType, String nodeId, String nodeName,
            String orgId,
            InfoNodeStatus parentNodeStatus) {
        String description = "";
        int status = BizConstants.NODE_STATUS_UNKNOWN;
        int rangeNum = dicNodeType.getRangeNum();
        /* 不需检查的 或 区间不合法的 */
        if(dicNodeType.getCheckFlag() == 0 || rangeNum < 2) {
            status = BizConstants.NODE_STATUS_UNCHECKED;
        }
        else if(value == BizConstants.UNKNOWN_VALUE) {
            status = BizConstants.NODE_STATUS_UNKNOWN;
        }
        else {
            Integer threshold1 = dicNodeType.getThreshold1();
            Integer threshold2 = dicNodeType.getThreshold2();
            Integer compareMode1 = dicNodeType.getCompareMode1();
            Integer compareMode2 = dicNodeType.getCompareMode2();
            if(checkThreshold(value, threshold1, compareMode1)) {
                status = dicNodeType.getRangeStatus1();
                description = MessageFormat.format(dicNodeType.getRangeRemark1(), threshold1);
            } else {
                status = dicNodeType.getRangeStatus2();
                description = MessageFormat.format(dicNodeType.getRangeRemark2(), threshold1);
                /* 判断区间数 */
                if (rangeNum == 3) {
                    if (checkThreshold(value, threshold2, compareMode2)) {
                        status = dicNodeType.getRangeStatus3();
                        description = MessageFormat.format(dicNodeType.getRangeRemark3(), threshold2);
                    }
                }
            }
        }
        /* 判断父节点的状态 */
        checkParentNodeStatus(status, checkTime, parentNodeStatus);

        return nodeStatusService.makeNodeStatus(
                        dicNodeType, nodeId, nodeName,
                        orgId,
                        status, parentNodeStatus.getNodeId(), description, checkTime);
    }


    /**
     * 根据值和阈值判断状态
     * @param value
     * @param threshold
     * @param compareMode
     * @return
     */
    private static boolean checkThreshold(Integer value, Integer threshold, Integer compareMode) {

        //  1为判断值是否小于等于阈值
        //  2为判断值是否小于阈值
        //  3为判断值是否大于等于阈值
        //  4为判断值是否大于阈值
        //  5为判断值是否等于阈值
        switch (compareMode) {
            case 1:
                return value <= threshold;
            case 2:
                return value < threshold;
            case 3:
                return value >= threshold;
            case 4:
                return value > threshold;
            case 5:
                return value == threshold;
            default:
                break;
        }

        return false;
    }
    /**
     * 根据传入的异常区间判断状态
     * @param checkTime
     * @param dicNodeType
     * @param nodeId
     * @param nodeName
     * @param orgId
     * @param parentNodeStatus
     * @return
     */
    public static InfoNodeStatus checkByRange(
            Integer range, Date checkTime,
            DicNodeType dicNodeType, String nodeId, String nodeName,
            String orgId,
            InfoNodeStatus parentNodeStatus) {
        String description = "";
        Integer status = BizConstants.NODE_STATUS_UNKNOWN;
        /* 不需检查的 */
        if(dicNodeType.getCheckFlag() == 0) {
            status = BizConstants.NODE_STATUS_UNCHECKED;
        }
        else if(range == BizConstants.STATUS_RANGE_UNKNOWN) {
            status = BizConstants.NODE_STATUS_UNKNOWN;
        }
        else {

            if (range == 1) {
                status = dicNodeType.getRangeStatus1();
                description = dicNodeType.getRangeRemark1();
            } else if (range == 2) {
                status = dicNodeType.getRangeStatus2();
                description = dicNodeType.getRangeRemark2();
            } else if (range == 3) {
                status = dicNodeType.getRangeStatus3();
                description = dicNodeType.getRangeRemark3();
            }

            checkParentNodeStatus(status, checkTime, parentNodeStatus);
        }

        return nodeStatusService.makeNodeStatus(
                dicNodeType, nodeId, nodeName,
                orgId,
                status, parentNodeStatus.getNodeId(), description, checkTime);
    }

    /**
     * 判断父节点状态
     * @param status
     * @param checkTime
     * @param parentNodeStatus
     */
    public static void checkParentNodeStatus(int status, Date checkTime, InfoNodeStatus parentNodeStatus) {
        /* 判断父节点的状态 */
        if(status > parentNodeStatus.getStatus()) {
            parentNodeStatus.setStatus(status);
            parentNodeStatus.setStatusCheckTime(checkTime);
        }
    }
}
